package Utils;

use DateTime;
use Log;

sub now {
    my $dt = DateTime->now;
    my $now = $dt->iso8601();
    return $now;
}

# defensive programming: internal error if values are not allowed
sub defense_valid {
    my $file = shift;
    my $line = shift;
    my $name = shift;
    my $value = shift;
    my @ret = grep { $value = $_ } @_;
    if(@ret == 0) {
	my $l = Log->instance();
	$l->xinternal("e78a95e5-1e65-3bce-8046-a20097d01c52",
			     $l->format($file,$line,"value not allowed for '$name' = '$value' while allowed values are: ".join(",",@_)));
    }
}

sub defense_between {
    my $file = shift;
    my $line = shift;
    my $name = shift;
    my $value = shift;
    my $min = shift;
    my $max = shift;
    if(($value < $min) || ($value > $max)) {
	my $l = Log->instance();
	$l->xinternal("79ccc76e-0f2d-3eaa-a1c1-581df3074bef",
		      $l->format($file,$line,"value not allowed for '$name' = '$value' while allowed values are >= $min and <= $max: "));
    }
}



1;
